﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

namespace MyClass
{
    /// <summary>
    /// Js 的摘要说明
    /// </summary>
    public class JavaScriptHelper
    {
        public JavaScriptHelper()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }

        #region 打开新窗体
        /// <summary>
        /// 打开新窗体
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static void OpenNewFrom(Page page, string url)
        {
            string Script = "<script>window.open('" + url + "');</script>";
            Type CSType = page.GetType();
            ClientScriptManager objClientScript = page.ClientScript;
            objClientScript.RegisterStartupScript(CSType, "", Script);
        }
        #endregion

        #region 显示消息提示对话框
        /// <summary>
        /// 显示消息提示对话框
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        /// <param name="msg">提示信息</param>
        public static void ShowMsg(Page page, string msg)
        {
            Type CSType = page.GetType();
            ClientScriptManager objClientScript = page.ClientScript;
            objClientScript.RegisterStartupScript(CSType, "", "<script language='javascript' defer>alert('" + msg.ToString() + "');</script>");
        }
        #endregion 

        #region 进行页面跳转
        /// <summary>
        /// 进行页面跳转
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        /// <param name="url">跳转的目标URL</param>
        public static void Redirect(Page page, string url)
        {
            StringBuilder Builder = new StringBuilder();
            Builder.Append("<script language='javascript' >");
            Builder.AppendFormat("location.href='{0}'", url);
            Builder.Append("</script>");

            Type CSType = page.GetType();
            ClientScriptManager objClientScript = page.ClientScript;
            objClientScript.RegisterStartupScript(CSType, "message", Builder.ToString());
        }
        #endregion


        #region  显示消息提示对话框，并进行页面跳转
        /// <summary>
        /// 显示消息提示对话框，并进行页面跳转
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        /// <param name="msg">提示信息</param>
        /// <param name="url">跳转的目标URL</param>
        public static void ShowAndRedirects(System.Web.UI.Page page, string msg, string url)
        {
            StringBuilder Builder = new StringBuilder();
            Builder.Append("<script language='javascript' defer>");
            Builder.AppendFormat("alert('{0}');", msg);
            Builder.AppendFormat("top.location.href='{0}'", url);
            Builder.Append("</script>");
            page.ClientScript.RegisterStartupScript(page.GetType(), "message", Builder.ToString());

        }
        #endregion

        #region 输出自定义脚本信息
        /// <summary>
        /// 输出自定义脚本信息
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        /// <param name="script">输出脚本</param>
        public static void ResponseScript(System.Web.UI.Page page, string script)
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), "message", "<script language='javascript' defer>" + script + "</script>");

        }
        #endregion

        #region Show 弹出确定、取消对话框
        /// <summary>
        /// 弹出确定、取消对话框
        /// </summary>
        /// <param name="registerControl">要点击的按钮</param>
        /// <param name="message">弹出的消息</param>
        /// <param name="wrongLogical">点击取消执行的方法名</param>
        public static void ShowConFirm(WebControl registerControl, string message, EventHandler wrongLogical)
        {
            RegisterShowControl(registerControl, message, "onclick", wrongLogical);
        }

        /// <summary>
        /// 注册JavaScript事件
        /// </summary>
        /// <param name="registerControl">WebControl</param>
        /// <param name="message">显示信息</param>
        /// <param name="clientEventName">事件名称</param>
        /// <param name="wrongLogical"></param>
        public static void RegisterShowControl(WebControl registerControl, string message, string clientEventName, EventHandler wrongLogical)
        {
            System.Web.UI.Page page = registerControl.Page;

            string rightLogicalCode = page.ClientScript.GetPostBackEventReference(registerControl, string.Empty);
            string wrongLogicalCode = string.Empty;

            if (wrongLogical != null)
            {
                Button btnCancel = new Button();
                btnCancel.Attributes["style"] = "display:none;";
                btnCancel.Click += new EventHandler(wrongLogical);
                page.Form.Controls.Add(btnCancel);

                wrongLogicalCode = "var _confrim_cancel_control_ = window.document.getElementById('" + btnCancel.ClientID + "'); if(_confrim_cancel_control_){_confrim_cancel_control_.click();}";
            }

            string jscodeA = "javascript:if(confirm('" + message + "')){function _confirm_(){" + rightLogicalCode + ";";
            string jscodeB = "} _confirm_();}else{ " + (wrongLogical != null ? wrongLogicalCode : "return false;") + " ; return false;}";

            if (registerControl.Attributes[clientEventName] != null && registerControl.Attributes[clientEventName].Contains("_confirm_()"))
                registerControl.Attributes[clientEventName] = string.Empty;

            registerControl.Attributes[clientEventName] = jscodeA + registerControl.Attributes[clientEventName] + jscodeB;
        }
        #endregion

 
     
    }
}